home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / mosml / src / mosmllib / Word8Vector.mlp < prev    next >
Encoding:
Text File  |  1996-07-03  |  3.0 KB  |  108 lines  |  [TEXT/R*ch]

  1. (* Word8Vector.mlp *)
  2.  
  3. prim_eqtype vector;
  4. type elem = Word8.word;
  5.  
  6. local 
  7.     prim_val vector_ : int -> vector                 = 1 "create_string";
  8.     prim_val sub_    : vector -> int -> elem         = 2 "get_nth_char";
  9.     prim_val update_ : vector -> int -> elem -> unit = 3 "set_nth_char";
  10.     prim_val blit_   : vector -> int -> vector -> int -> int -> unit 
  11.                                                      = 5 "blit_string";
  12. in 
  13.  
  14. prim_val length : vector -> int = 1 "string_length";
  15.  
  16. #include "../config/m.h"
  17. #ifdef SIXTYFOUR
  18. val maxLen = 144115188075855863;      (* = (2^54-1)*8-1, with 64 bit *)
  19. #else
  20. val maxLen = 16777211;          (* = (2^22-1)*4-1, with 32 bit *)
  21. #endif
  22.  
  23. fun fromList (vs : elem list) =
  24.   let val n = List.length vs
  25.       val a = if n > maxLen then raise Size else vector_ n
  26.       fun init [] i = ()
  27.         | init (v::vs) i = (update_ a i v; init vs (i+1))
  28.   in (init vs 0; a) end;
  29.  
  30. fun tabulate(n, f : int -> elem) =
  31.   if n < 0 orelse n > maxLen then raise Size else
  32.   let val a = vector_ n
  33.       fun init i = if i >= n then () else (update_ a i (f i); init (i+1))
  34.   in (init 0; a) end;
  35.  
  36. fun sub(v, i) =
  37.   if i < 0 orelse i >= length v then raise Subscript 
  38.   else sub_ v i;
  39.  
  40. fun extract (vec, i, slicelen) = 
  41.     let val n = case slicelen of NONE => length vec - i | SOME n => n
  42.     val newvec = if i<0 orelse n<0 orelse i+n > length vec then
  43.                      raise Subscript
  44.              else
  45.              vector_ n
  46.     in blit_ vec i newvec 0 n; newvec end;
  47.  
  48. fun concat vecs =
  49.     let fun acc [] len       = len
  50.       | acc (v1::vr) len = acc vr (length v1 + len)
  51.     val len = acc vecs 0
  52.     val newvec = if len > maxLen then raise Size else vector_ len 
  53.     fun copyall to []       = ()
  54.       | copyall to (v1::vr) = 
  55.         let val len1 = length v1
  56.         in blit_ v1 0 newvec to len1; copyall (to+len1) vr end
  57.     in copyall 0 vecs; newvec end;
  58.  
  59. fun foldl f e a = 
  60.     let    val stop = length a
  61.     fun lr j res = if j < stop then lr (j+1) (f(sub_ a j, res))
  62.                else res
  63.     in lr 0 e end
  64.  
  65. fun foldr f e a =
  66.     let    fun rl j res = if j >= 0 then rl (j-1) (f(sub_ a j, res))
  67.                else res
  68.     in rl (length a - 1) e end
  69.  
  70. fun app f a = 
  71.     let val stop = length a
  72.     fun lr j = if j < stop then (f(sub_ a j); lr (j+1))
  73.            else ()
  74.     in lr 0 end
  75.  
  76. fun sliceend (a, i, NONE) = 
  77.         if i<0 orelse i>length a then raise Subscript
  78.     else length a
  79.   | sliceend (a, i, SOME n) = 
  80.     if i<0 orelse n<0 orelse i+n>length a then raise Subscript
  81.     else i+n;
  82.  
  83. fun foldli f e (slice as (a, i, _)) = 
  84.     let fun loop stop =
  85.         let fun lr j res = 
  86.         if j < stop then lr (j+1) (f(j, sub_ a j, res))
  87.         else res
  88.         in lr i e end
  89.     in loop (sliceend slice) end;
  90.  
  91. fun foldri f e (slice as (a, i, _)) = 
  92.     let fun loop start =
  93.         let fun rl j res = 
  94.             if j >= i then rl (j-1) (f(j, sub_ a j, res))
  95.             else res
  96.         in rl start e end;
  97.     in loop (sliceend slice - 1) end
  98.  
  99. fun appi f (slice as (a, i, _)) = 
  100.     let fun loop stop = 
  101.         let    fun lr j = 
  102.             if j < stop then (f(j, sub_ a j); lr (j+1)) 
  103.             else ()
  104.         in lr i end
  105.     in loop (sliceend slice) end;
  106. end
  107.  
  108.